package com.xiaren.util;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JTextPane;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;

import com.xiaren.common.ObjectConstant;
import com.xiaren.ui.edit.FileEdit;

/**
 * 颜色选择
 *
 * @author xiaren
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public class ColorComboBox extends JComboBox {
    public static final String[] ARRAY_COLOR;

    static {
        ARRAY_COLOR = new String[]{"black", "red",
                "green", "white", "yellow", "gray"};
    }

    private FileEdit fileedit;

    public ColorComboBox() {
        super(ARRAY_COLOR);
        this.setRenderer(new ColorComboBoxRenderer());
        this.setSize(30, 30);
        this.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ColorComboBox source = (ColorComboBox) e.getSource();
                Object value = source.getSelectedItem();
                fileedit = (FileEdit) ObjectConstant.mainEdit.getSelectedComponent();
                if (fileedit == null) {
                    return;
                }
                MutableAttributeSet attr = new SimpleAttributeSet();
                if ("black".equals(value)) {
                    StyleConstants.setForeground(attr, Color.black);
                } else if ("red".equals(value)) {
                    StyleConstants.setForeground(attr, Color.red);
                } else if ("green".equals(value)) {
                    StyleConstants.setForeground(attr, Color.green);
                } else if ("yellow".equals(value)) {
                    StyleConstants.setForeground(attr, Color.yellow);
                } else if ("white".equals(value)) {
                    StyleConstants.setForeground(attr, Color.white);
                } else if ("gray".equals(value)) {
                    StyleConstants.setForeground(attr, Color.gray);
                }
                StyledEditorUtil.setCharacterAttributes((JEditorPane) fileedit.getTypeFile(), attr, false);
                source.setSelectedIndex(0);
            }
        });
    }


    public class ColorComboBoxRenderer extends BasicComboBoxRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                                                      int index, boolean isSelected, boolean cellHasFocus) {
            // TODO Auto-generated method stub

            Component c = super.getListCellRendererComponent(list, value,
                    index, isSelected, cellHasFocus);
            if ("black".equals(value)) {
                c.setBackground(Color.black);
            } else if ("pink".equals(value)) {
                c.setBackground(Color.pink);
            } else if ("green".equals(value)) {
                c.setBackground(Color.green);
            } else if ("yellow".equals(value)) {
                c.setBackground(Color.yellow);
            } else if ("white".equals(value)) {
                c.setBackground(Color.white);
            }
            return c;
        }
    }

//	class ColorLabel extends JLabel{
//		
//		JMenuItem redTextItem = new JMenuItem(
//				new StyledEditorKit.ForegroundAction("Red", Color.red));
//		JMenuItem orangeTextItem = new JMenuItem(
//				new StyledEditorKit.ForegroundAction("Orange", Color.orange));
//		JMenuItem yellowTextItem = new JMenuItem(
//				new StyledEditorKit.ForegroundAction("Yellow", Color.yellow));
//		JMenuItem greenTextItem = new JMenuItem(
//				new StyledEditorKit.ForegroundAction("Green", Color.green));
//		JMenuItem blueTextItem = new JMenuItem(
//				new StyledEditorKit.ForegroundAction("Blue", Color.blue));
//		JMenuItem cyanTextItem = new JMenuItem(
//				new StyledEditorKit.ForegroundAction("Cyan", Color.cyan));
//		JMenuItem magentaTextItem = new JMenuItem(
//				new StyledEditorKit.ForegroundAction("Magenta", Color.magenta));
//		JMenuItem blackTextItem = new JMenuItem(
//				new StyledEditorKit.ForegroundAction("Black", Color.black));
//	}
}
